package org.chen.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.chen.model.elastic.ArticleIndex;
import org.chen.model.elastic.SearchResult;
import org.chen.model.mongodb.ArticleDetailVO;
import org.chen.model.mongodb.ArticleDocument;
import org.chen.model.quey.UserArticleQueryParam;
import org.chen.response.Result;
import org.chen.service.impl.ArticleStorageService;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/articles")
@Slf4j
public class ArticleController {
    private final ArticleStorageService articleService;

    public ArticleController(ArticleStorageService articleService) {
        this.articleService = articleService;
    }

    // 分页获取所有文章
    @PostMapping
    public Page<ArticleIndex> getAllArticles(
            @RequestBody UserArticleQueryParam request) {

        return articleService.getArticleList(request);
    }

    // 搜索接口（带分页）
    @GetMapping("/search")
    public SearchResult search(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return articleService.searchArticles(keyword, page, size);
    }

    // 获取最新文章
    @GetMapping("/latest")
    public Page<ArticleIndex> getLatestArticles(
            @RequestParam(defaultValue = "10") int limit) {
        return articleService.getLatestArticles(limit);
    }

    // 获取热门文章
    @GetMapping("/popular")
    public Page<ArticleIndex> getPopularArticles(
            @RequestParam(defaultValue = "10") int limit) {
        return articleService.getPopularArticles(limit);
    }

    // 按作者搜索
    @GetMapping("/author/{author}")
    public Page<ArticleIndex> getArticlesByAuthor(
            @PathVariable String author,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return articleService.findByAuthor(author, page, size);
    }

    // 获取文章详情（返回JSON格式）
    @GetMapping("/{id}")
    public Result<ArticleDetailVO> getArticle(@PathVariable String id) {
        // 1. 获取文章详情
        ArticleDetailVO articleDetail = articleService.getArticleDetail(id);
        Long articleViewCount = articleService.getArticleViewCount(id);
        articleDetail.getArticle().setViewCount(articleViewCount);
        return Result.success(articleDetail);
    }

    //获取阅读量
    @GetMapping("/{id}/viewCount")
    public Result<Long> getArticleViewCount(@PathVariable String id) {
        Long viewCount = articleService.getArticleViewCount(id);
        return Result.success(viewCount);
    }

    // 获取相关文章
    @GetMapping("/{id}/related")
    public List<ArticleDocument> getRelatedArticles(@PathVariable String id) {
        return articleService.getRelatedArticles(id);
    }
}

